package com.googlecode.moneyManager.dao.hib;

import com.googlecode.moneyManager.dao.CustomerDAO;
import com.googlecode.moneyManager.exceptions.DataAccessException;
import com.googlecode.moneyManager.model.Customer;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.hibernate.search.query.dsl.QueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import javax.management.Query;
import java.util.List;

/**
 * @author Sergiy Doroshenko
 *         Aug 26, 2008 6:57:06 PM
 */
public class CustomerDAOHib extends HibernateDaoSupport implements CustomerDAO {

    @SuppressWarnings("unchecked")
    public List<Customer> getUsersByName(String fname, String sname) {
        return getSession().createQuery("from Customer u where u.FName = ? and u.SName = ?")
                .setString(0, fname)
                .setString(1, sname)
                .list();
    }

    @Override
    public List<Customer> searchUserByName(String fname, String sname) {
        FullTextSession fullTextSession = Search.getFullTextSession(getSession());
        Transaction tx = fullTextSession.beginTransaction();
        // create native Lucene query unsing the query DSL
        // alternatively you can write the Lucene query using the Lucene query parser
        // or the Lucene programmatic API. The Hibernate Search DSL is recommended though
        QueryBuilder qb = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Customer.class).get();
        org.apache.lucene.search.Query query = qb.keyword()
                .onFields("sname", "fname")
                .matching(fname).createQuery();
        // wrap Lucene query in a org.hibernate.Query
        org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(query, Customer.class);
        // execute search
        List result = hibQuery.list();
        tx.commit();
        return result;
    }

    public String create(Customer t) throws DataAccessException {
        return null;
    }

    public void delete(Customer t) throws DataAccessException {
    }

    public Customer get(String id) throws DataAccessException {
        return null;
    }

    public List<Customer> getAll() throws DataAccessException {
        return null;
    }

    public void merge(Customer t) throws DataAccessException {
    }

    public void saveOrUpdate(Customer t) throws DataAccessException {
    }

}
